package com.power.sifa.std.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.std.model.Std_matter;
import com.power.sifa.std.model.pageInfo.Std_matterPageInfo;
import com.power.sifa.std.api.IStd_matterFeign;
import com.power.sifa.std.service.Std_matterService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   申请事项类型控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Std_matterController",tags={"申请事项类型"})
public class Std_matterController implements IStd_matterFeign{

    @Resource
    private Std_matterService std_matterService;


    @Override
    @ApiOperation("查询申请事项类型分页列表")
    @SaCheckPermission("std_matter_list")
    public ResponseDTO list(Std_matterPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Std_matter> page = this.std_matterService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Std_matter std_matter : page) {
                std_matter.geneText();
                list.add(std_matter);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看申请事项类型")
    @SaCheckPermission("std_matter_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Std_matter std_matter  = std_matterService.getById(id);
        if (std_matter != null) {
            std_matter.geneText();
        }
        return ResponseDTO.succData(std_matter);
    }

    @Override
    @ApiOperation("删除申请事项类型")
    @SaCheckPermission("std_matter_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        std_matterService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改申请事项类型")
    @SaCheckPermission("std_matter_save")
    public ResponseDTO save(Std_matter std_matter) {
        Assert.notNull(std_matter, "std_matter不能为空");
        std_matterService.saveOrUpdate(std_matter);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除申请事项类型")
    @SaCheckPermission("std_matter_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        std_matterService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("std_matter_list")
    public void output(HttpServletResponse response, Std_matterPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "申请事项类型";
        String[] heads = {"grpid#code","事项编码","实施编码","事项名称","顺序号","删除标志","分组id","事项类型","流程英文标识"};
        String[] fields = {"id","itemcode","handleitem","itemname","sortnum","deleted","grpid","itemtype","processkey"};
        Page<Std_matter> page = this.std_matterService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "申请事项类型";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"grpid#code","事项编码","实施编码","事项名称","顺序号","删除标志","分组id","事项类型","流程英文标识"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","itemcode","handleitem","itemname","sortnum","deleted","grpid","itemtype","processkey"};
        List<Std_matter> list = (List<Std_matter>)ExcelUtil.Import(fields, file, Std_matter.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.std_matterService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

}
